package com.GlobalPaint.app.ui.Home.friend;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.EventBusben;
import com.GlobalPaint.app.ui.location.MyFriendsFragment;
import com.GlobalPaint.app.utils.MyViewPager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.finalteam.toolsfinal.adapter.FragmentAdapter;

public class FriendActivity extends AppCompatActivity {
    @BindView(R.id.iv_goback)
    ImageView ivGoback;
    @BindView(R.id.tv_common_center)
    TextView tvCommonCenter;
    @BindView(R.id.tv_haoyou)
    TextView tvHaoyou;
    @BindView(R.id.vw_haoyou)
    View vwHaoyou;
    @BindView(R.id.ll_haoyou)
    LinearLayout llHaoyou;
    @BindView(R.id.tv_fensi)
    TextView tvFensi;
    @BindView(R.id.vw_fensi)
    View vwFensi;
    @BindView(R.id.ll_fensi)
    LinearLayout llFensi;
    @BindView(R.id.tv_guanzhu)
    TextView tvGuanzhu;
    @BindView(R.id.vw_guanzhu)
    View vwGuanzhu;
    @BindView(R.id.ll_guanzhu)
    LinearLayout llGuanzhu;
    @BindView(R.id.view_pager)
    MyViewPager viewPager;
    @BindView(R.id.img_popup)
    ImageView imgPopup;
    @BindView(R.id.tv_qunliao)
    TextView tvQunliao;
    @BindView(R.id.vw_qunliao)
    View vwQunliao;
    @BindView(R.id.ll_qunliao)
    LinearLayout llQunliao;
    @BindView(R.id.tv_fresh)
    TextView tvFresh;
    private MyFriendsFragment myFriendsFragment;
    private FensiFragment fensiFragment;
    private GuanzhuFragment guanzhuFragment;
    private QunLiaoFragment qunLiaoFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend);
        ButterKnife.bind(this);
        tvCommonCenter.setText("圈子");
        EventBus.getDefault().register(this);
        imgPopup.setVisibility(View.VISIBLE);
        //初始化TabLayout的title数据集
        List<String> titles = new ArrayList<>();
        titles.add("");
        titles.add("");
        titles.add("");
        titles.add("");
        myFriendsFragment = new MyFriendsFragment();
        qunLiaoFragment = new QunLiaoFragment();
        fensiFragment = new FensiFragment();
        guanzhuFragment = new GuanzhuFragment();
        //初始化ViewPager的数据集
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(myFriendsFragment);
        fragments.add(qunLiaoFragment);
        fragments.add(fensiFragment);
        fragments.add(guanzhuFragment);
        //创建ViewPager的adapter
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(4);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void FirstEvent(EventBusben message) {
        String msg = message.getNr();

        if (msg == "haoyou") {
            myFriendsFragment.job();
        }
        if (msg == "guanzhu") {
            guanzhuFragment.job();
        }
        if (msg == "qunliao") {
            qunLiaoFragment.job();
        }
    }

    @OnClick({R.id.iv_goback, R.id.ll_haoyou, R.id.ll_fensi, R.id.ll_guanzhu, R.id.img_popup, R.id.ll_qunliao,R.id.tv_fresh})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_goback:
                finish();
                break;
            case R.id.ll_haoyou:
                tvFensi.setTextColor(Color.rgb(144, 144, 144));
                tvHaoyou.setTextColor(Color.rgb(37, 168, 37));
                tvGuanzhu.setTextColor(Color.rgb(144, 144, 144));
                tvQunliao.setTextColor(Color.rgb(144, 144, 144));
                vwHaoyou.setVisibility(View.VISIBLE);
                vwFensi.setVisibility(View.GONE);
                vwGuanzhu.setVisibility(View.GONE);
                vwQunliao.setVisibility(View.GONE);
                imgPopup.setVisibility(View.VISIBLE);
                tvFresh.setVisibility(View.GONE);
                viewPager.setCurrentItem(0);
                break;
            case R.id.ll_fensi:
                tvHaoyou.setTextColor(Color.rgb(144, 144, 144));
                tvGuanzhu.setTextColor(Color.rgb(144, 144, 144));
                tvFensi.setTextColor(Color.rgb(37, 168, 37));
                tvQunliao.setTextColor(Color.rgb(144, 144, 144));
                vwHaoyou.setVisibility(View.GONE);
                vwGuanzhu.setVisibility(View.GONE);
                vwFensi.setVisibility(View.VISIBLE);
                vwQunliao.setVisibility(View.GONE);
                imgPopup.setVisibility(View.GONE);
                tvFresh.setVisibility(View.GONE);
                viewPager.setCurrentItem(2);
                break;
            case R.id.ll_guanzhu:
                tvHaoyou.setTextColor(Color.rgb(144, 144, 144));
                tvGuanzhu.setTextColor(Color.rgb(37, 168, 37));
                tvFensi.setTextColor(Color.rgb(144, 144, 144));
                tvQunliao.setTextColor(Color.rgb(144, 144, 144));
                vwFensi.setVisibility(View.GONE);
                vwGuanzhu.setVisibility(View.VISIBLE);
                vwHaoyou.setVisibility(View.GONE);
                vwQunliao.setVisibility(View.GONE);
                imgPopup.setVisibility(View.GONE);
                tvFresh.setVisibility(View.GONE);
                viewPager.setCurrentItem(3);
                break;
            case R.id.img_popup:
                Intent intent = new Intent(this, popupActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_qunliao:
                tvHaoyou.setTextColor(Color.rgb(144, 144, 144));
                tvGuanzhu.setTextColor(Color.rgb(144, 144, 144));
                tvFensi.setTextColor(Color.rgb(144, 144, 144));
                tvQunliao.setTextColor(Color.rgb(37, 168, 37));
                vwHaoyou.setVisibility(View.GONE);
                vwGuanzhu.setVisibility(View.GONE);
                vwFensi.setVisibility(View.GONE);
                vwQunliao.setVisibility(View.VISIBLE);
                imgPopup.setVisibility(View.GONE);
                tvFresh.setVisibility(View.VISIBLE);
                tvFresh.setText("创建群聊");
                viewPager.setCurrentItem(1);
                break;
            case R.id.tv_fresh:
                Intent intent1 = new Intent(this, QunLiaoActivity.class);
                startActivity(intent1);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
